package com.jianbing.shasha.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.jianbing.common.utils.SecurityUtils;
import com.jianbing.shasha.constant.ShashaTestAnswerConstant;
import com.jianbing.shasha.domain.ShashaTestQues;
import com.jianbing.shasha.domain.ShashaUserConsolidateQues;
import com.jianbing.shasha.domain.ShashaUserRecordVo;
import com.jianbing.shasha.mapper.ShashaUserRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianbing.shasha.mapper.ShashaQuestionBankMapper;
import com.jianbing.shasha.domain.ShashaQuestionBank;
import com.jianbing.shasha.service.IShashaQuestionBankService;

/**
 * 题库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-09
 */
@Service
public class ShashaQuestionBankServiceImpl implements IShashaQuestionBankService 
{
    @Autowired
    private ShashaQuestionBankMapper shashaQuestionBankMapper;
    @Autowired
    private ShashaUserRecordMapper shashaUserRecordMapper;
    /**
     * 查询题库
     * 
     * @param id 题库主键
     * @return 题库
     */
    @Override
    public ShashaQuestionBank selectShashaQuestionBankById(Long id)
    {
        return shashaQuestionBankMapper.selectShashaQuestionBankById(id);
    }

    /**
     * 查询题库列表
     * 
     * @param shashaQuestionBank 题库
     * @return 题库
     */
    @Override
    public List<ShashaQuestionBank> selectShashaQuestionBankList(ShashaQuestionBank shashaQuestionBank)
    {
        return shashaQuestionBankMapper.selectShashaQuestionBankList(shashaQuestionBank);
    }

    /**
     * 新增题库
     * 
     * @param shashaQuestionBank 题库
     * @return 结果
     */
    @Override
    public int insertShashaQuestionBank(ShashaQuestionBank shashaQuestionBank)
    {
        return shashaQuestionBankMapper.insertShashaQuestionBank(shashaQuestionBank);
    }

    /**
     * 修改题库
     * 
     * @param shashaQuestionBank 题库
     * @return 结果
     */
    @Override
    public int updateShashaQuestionBank(ShashaQuestionBank shashaQuestionBank)
    {
        return shashaQuestionBankMapper.updateShashaQuestionBank(shashaQuestionBank);
    }

    /**
     * 批量删除题库
     * 
     * @param ids 需要删除的题库主键
     * @return 结果
     */
    @Override
    public int deleteShashaQuestionBankByIds(Long[] ids)
    {
        return shashaQuestionBankMapper.deleteShashaQuestionBankByIds(ids);
    }

    /**
     * 删除题库信息
     * 
     * @param id 题库主键
     * @return 结果
     */
    @Override
    public int deleteShashaQuestionBankById(Long id)
    {
        return shashaQuestionBankMapper.deleteShashaQuestionBankById(id);
    }
    
    
    /**
     * 获取指定数量的题目
     * 
     * @param levelId
     * @param count
     * @return java.util.List<com.jianbing.shasha.domain.ShashaTestQues>
     * @author zheng
     * @create 2024/6/10
     **/
    @Override
    public List<ShashaTestQues> getQuesWithCount(Long levelId, Long count) {
        Integer levelNumbers = Math.toIntExact(count);
        Long userId = SecurityUtils.getUserId();
        List<ShashaTestQues> list = shashaQuestionBankMapper.getQuesWithCount(levelId);
        String answer = null;
        Long type =  (levelId-1)%4;
        if(type==0){
            answer = ShashaTestAnswerConstant.HAPPY;
        }else if (type==1){
            answer = ShashaTestAnswerConstant.SAD;
        } else if (type==2) {
            answer = ShashaTestAnswerConstant.ANGRY;
        }else {
            answer = ShashaTestAnswerConstant.FEAR;
        }
        String finalAnswer = answer;
        list.stream().forEach((shashaTestQues)->shashaTestQues.setAnswerPath(finalAnswer));
        Long number = shashaUserRecordMapper.selectDoQuesCountByUserIdAndLevel(userId,levelId);
        Integer beginNumber = (int) (number - number % levelNumbers);
        if(number>=list.size()){
            Collections.shuffle(list);
            return list.subList(0,count.intValue());
        } else if (beginNumber + levelNumbers <= list.size()) {
            return list.subList(beginNumber, beginNumber + levelNumbers);
        }else{
            Integer num =beginNumber + levelNumbers -  list.size();
            Collections.shuffle(list);
            List<ShashaTestQues> res = new ArrayList<>(list.subList(beginNumber,list.size()));
            list.removeAll(res);
            res.addAll(list.subList(0,num));
            return res;
        }
    }

//    @Override
//    public List<Long> getConsolidate(ShashaUserConsolidateQues shashaUserConsolidateQues) {
//        Long level = shashaUserConsolidateQues.getLevel();
//        Long userId = shashaUserConsolidateQues.getUserId();
//        List<Long> all;
//        if(level % 8 ==1){
//            return null;
//        }else{
//            all =  shashaUserRecordMapper.selectWrongRecordByUserId(userId);
//        }
//        return all;
//    }

    @Override
    public List<Long> getConsolidate(ShashaUserConsolidateQues shashaUserConsolidateQues) {
        Long level = shashaUserConsolidateQues.getLevel();
        Long userId = shashaUserConsolidateQues.getUserId();
        List<Long> all = new ArrayList<>();
        if(level%8==1){
            return null;
        }else{
            List<ShashaUserRecordVo> tmp = shashaUserRecordMapper
                    .selectTargetQuesByUserIdAndLevel(userId,level-1,10,level%4==2?null:1);
            level--;
            for(;level%4!=1;level--){
                List<ShashaUserRecordVo> vos = shashaUserRecordMapper.selectTargetQuesByUserIdAndLevel(userId, level - 1, 1, level % 4 == 2 ? null : 1);
                if(vos == null || vos.size()==0){
                    break;
                }
                all.add(vos.get(0).getQuestionId());
            }
            int count=0;
            for(ShashaUserRecordVo recordVo:tmp){
                all.add(recordVo.getQuestionId());
                count++;
                if(all.size()>=10 ||(count>=3 && (!chargeNot1(recordVo.getCorrect())|| !chargeNot1(recordVo.getReview())))){
                    break;
                }
            }
            Collections.shuffle(all);
            return all;
        }
    }

    private boolean chargeNot1(Integer num){
        return num==null||num==0;
    }

}
